package com.hw.spark.configuration;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.huaban.analysis.jieba.JiebaSegmenter;

@Configuration
public class SparkConfiguration {
	
	@Value("${spring.application.name}")
	private String appName;
	@Value("${spark.master}")
	private String master;

	@Bean("sparkConf")
	public SparkConf sparkConf() {
		return new SparkConf()
				// 设置模式为本地模式 [*] 为使用本机核数
				.setMaster(master)
//				设置监听器
				.set("spark.extraListeners", "com.hw.spark.listener.SparkSQLListener")
				// 设置应用名
				.setAppName(appName);
	}

	@Bean("sparkContext")
	@ConditionalOnBean(name="sparkConf")
	public JavaSparkContext javaSparkContext() {
		return new JavaSparkContext(sparkConf());
	}
	
	@Bean
	public JiebaSegmenter jiebaSegmenter(){
		return new JiebaSegmenter();
	}
}
